package com.zwps.common.controller.constant;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;

public class HttpHeaderConstant {

    public static final String HEADER_TOKEAN = "Authorization";
    public static final String HEADER_CAS_TOKEAN = "Token";

    public static final String HEADER_LOGIN_INFO = "Login-Info";
    public static final String HEADER_FEIGN_REQUEST = "Feign-Request";
    public static final String HEADER_REQUEST_ID = "Request-Id";

    public static final String HEADER_TOKEAN_OLD = "token";

    public static boolean isFeignRequest(HttpServletRequest request) {
        return getFeignRequestVuale().equals(request.getHeader(HEADER_FEIGN_REQUEST));
    }

    public static String getFeignRequestVuale() {
        return "true";
    }

    public static synchronized String getAndSetRequestId(HttpServletRequest request) {
        String requestId = getRequestId(request);
        if (StrUtil.isEmpty(requestId)) {
            requestId = UUID.fastUUID().toString(true);
            request.setAttribute(HEADER_REQUEST_ID, requestId);
        }
        return requestId;
    }

    public static synchronized String getRequestId(HttpServletRequest request) {
        String requestId = (String) request.getAttribute(HEADER_REQUEST_ID);
        if (StrUtil.isEmpty(requestId)) {
            return request.getHeader(HEADER_REQUEST_ID);
        }
        return requestId;
    }
}
